/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Presetacion;

import Negocio.Hoja;
import java.util.Vector;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author Ivan
 */
public class ModelArbol implements TreeModel {
    private boolean showAncestors;
    private Vector<TreeModelListener> treeModelListeners =
        new Vector<TreeModelListener>();
    private Hoja rootPerson;

    public ModelArbol(Hoja root) {
        showAncestors = false;
        rootPerson = root;
    }

    /**
     * Used to toggle between show ancestors/show descendant and
     * to change the root of the tree.
     */
    public void showAncestor(boolean b, Object newRoot) {
        showAncestors = b;
        Hoja oldRoot = rootPerson;
        if (newRoot != null) {
           rootPerson = (Hoja)newRoot;
        }
        fireTreeStructureChanged(oldRoot);
    }


//////////////// Fire events //////////////////////////////////////////////

    /**
     * The only event raised by this model is TreeStructureChanged with the
     * root as path, i.e. the whole tree has changed.
     */
    protected void fireTreeStructureChanged(Hoja oldRoot) {
        int len = treeModelListeners.size();
        TreeModelEvent e = new TreeModelEvent(this,
                                              new Object[] {oldRoot});
        for (TreeModelListener tml : treeModelListeners) {
            tml.treeStructureChanged(e);
        }
    }


//////////////// TreeModel interface implementation ///////////////////////

    /**
     * Adds a listener for the TreeModelEvent posted after the tree changes.
     */
    public void addTreeModelListener(TreeModelListener l) {
        treeModelListeners.addElement(l);
    }

    /**
     * Returns the child of parent at index index in the parent's child array.
     */
    public Object getChild(Object parent, int index) {
        Hoja p = (Hoja)parent;

        return p.getHijos(index);
    }

    /**
     * Returns the number of children of parent.
     */
    public int getChildCount(Object parent) {
        Hoja p = (Hoja)parent;
        return p.getCantidadHijos();
    }

    /**
     * Returns the index of child in parent.
     */
    public int getIndexOfChild(Object parent, Object child) {
//        try {
//            Hoja p = (Hoja) parent;
//            return p.getIndexOfHijo((Hoja) child);
//        } catch (ExceptionNoEsHijo ex) {
//            return 0;
//        }
        return 0;

    }

    /**
     * Returns the root of the tree.
     */
    public Object getRoot() {
        return rootPerson;
    }

    /**
     * Returns true if node is a leaf.
     */
    public boolean isLeaf(Object node) {
        Hoja p = (Hoja)node;
        if (showAncestors) {
//            return ((p.getFather() == null)
//                 && (p.getMother() == null));
        }
        int var=p.getCantidadHijos();
        boolean var2=p.getCantidadHijos() == 0;
        return p.getCantidadHijos() == 0;

    }

    /**
     * Removes a listener previously added with addTreeModelListener().
     */
    public void removeTreeModelListener(TreeModelListener l) {
        treeModelListeners.removeElement(l);
    }



    /**
     * Messaged when the user has altered the value for the item
     * identified by path to newValue.  Not used by this model.
     */
    public void valueForPathChanged(TreePath path, Object newValue) {
        Hoja nodo=(Hoja)path.getLastPathComponent();
        String valor=(String) newValue;

//        if(nodo.esInformacion()){
//            nodo.set(valor);
//        }
//        else
//            nodo.getElemento().setEtiqueta(valor);
//
//         System.out.println(nodo.resumen());

            }




}
